local PageView = class("PageView");
local CURRENT_MODULE = ...;
local dotindex = string.find(CURRENT_MODULE, "%.%w+$");
local currentpath = string.sub(CURRENT_MODULE, 1, dotindex-1);
local pageViewMarkPath = string.format("%s.PageViewMark", currentpath);
local PageViewMark = require(pageViewMarkPath);
--[[
{
    pageview = pv,
    col = 4, 默认为1
    row = 5, 默认为1
    columnSpace = 2, 默认为0
    rowSpace = 2,默认为0
    padding = {left = 0, right = 0, top = 0, bottom = 0},默认
    bCirc = true 默认为false
    autoMark = false
}
--]]

function PageView:ctor(args)
    self.pv = args.pv;
    self.pv:onTouch(handler(self,self.onTouch));
    self:setColumn(args.col);
    self:setRow(args.row);
    self:setColumnSpace(args.columnSpace);
    self:setRowSpace(args.rowSpace);
    self:setPadding(args.padding);
    self:setBCirc(args.bCirc);
    self:setAutoCreatePageViewMark(args.autoMark or false);
    self:setPageViewMarkParent(args.markParent);
    self.pageViewMark = nil;
    self.mark1png = args.mark1png;
    self.mark2png = args.mark2png;
end

function PageView:setColumn(col)
    if col and col >= 1 then
        self.pv.column_ = col;
    end
end

function PageView:setRow(row)
    if row and row >= 1 then
        self.pv.row_ = row;
    end
end

function PageView:setColumnSpace(colSpace)
    if colSpace then
        self.pv.columnSpace_ = colSpace;
    end
end

function PageView:setRowSpace(rowSpace)
    if rowSpace then
        self.pv.rowSpace_ = rowSpace;
    end
end

function PageView:setPadding(pad)
    if pad and table.nums(pad) > 0 then
        self.pv.padding_ = pad;
    end
end

function PageView:setBCirc(circ)
    if circ then
        self.pv.bCirc = circ;
    end
end

function PageView:setAutoCreatePageViewMark(mark)
    self.autoMark =  mark;
end


function PageView:getCurPageIdx()
    return self.pv:getCurPageIdx()
end

function PageView:setPageChangedCbk(func)
    self.pageChangedCbk = func;
end

function PageView:setItemClickedCbk(func)
    self.itemClickCbk = func;
end

function PageView:getPageCount()
    return self.pv:getPageCount();
end

function PageView:setCellMovedCbk(func)
    self.cellMovedCbk = func
end

function PageView:onTouch(event)
    local name = event.name;
    if name == "moved" then
        if self.cellMovedCbk then
            self.cellMovedCbk(event)
        end
    else
        local pageView = event.pageView;
        local pageIdx = event.pageIdx;
        if event.name == "clicked" then
            local item = event.item;
            local itemIdx = event.itemIdx;
            if self.itemClickCbk then
                self.itemClickCbk(event);
            end
        elseif event.name == "pageChange" then
            local curindex = self:getCurPageIdx()
            if curindex ~= pageIdx then
                if self.pageChangedCbk then
                    self.pageChangedCbk(event);
                end
                if self.autoMark and self.markParent then
                    self.pageViewMark:setSelectedIndex(event.pageIdx);
                end
            end
        end
    end
end


function PageView:addContent(content)
    local item = self.pv:newItem();
    local size = item:getContentSize();
    content:ignoreAnchorPointForPosition(false);
    content:setAnchorPoint(cc.p(0.5,0.5));
    content:setPosition(cc.p(size.width/2,size.height/2));
    item:addChild(content);
    content:setTouchEnabled(false);
    self.pv:addItem(item);
end

function PageView:reload()
    self.pv:reload();
    self:createMark();
end

function PageView:gotoPage(pageIdx, bSmooth, bLeftToRight)
    self.pv:gotoPage(pageIdx, bSmooth, bLeftToRight);
end

function PageView:createMark()
    if self.autoMark then
        self.pageViewMark = PageViewMark.new({count = self:getPageCount(),mark1png = self.mark1png, mark2png = self.mark2png});
        if self.markParent then
            self.markParent:addChild(self.pageViewMark);
        end
        self.pageViewMark:setMarkClickCbk(function(index)
            self:gotoPage(index,true,true);
        end)
    end
end

function PageView:setPageViewMarkParent(par)
    self.markParent = par;
end

function PageView:gotoNextPage()
    local itemscount = table.nums(self.pv.items_)
    local curindex = self:getCurPageIdx()
    local next = curindex + 1
    if next > itemscount then
        next = 1
    end
    self:gotoPage(next,true,true);
end



return PageView;

--endregion
